/*
* =====================================================================================
*
*       Filename:  makebreac.c
*
*    Description:  
*
*        Version:  1.0
*        Created:  2010年09月19日 10时45分58秒
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  YOUR NAME (), 
*        Company:  
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "me/stack.h"
#include "me/mem.h"
#include "me/assert.h"



char find(FILE * stream)
{
	while (1) {
		char p = getc(stream);
		switch(p) {
			case EOF:
				return ' ';
			case '{': 
			case '}':
			case '[':
			case ']':
			case '(':
			case ')':
				putc(p, stderr);
				return p;
			default:
				break;
		}
	}
}

bool compare(char left, char right)
{
	switch (left) {
		case '[':
			return (right== ']') ? true : false;
		case '{':
			return (right == '}') ? true : false;
		case '(':
			return (right == ')') ? true : false;
		default:
			break;
	}

	return false;
}

bool isleft(char p)
{
	return (p == '{' || p=='[' || p=='(') ? true : false;
}



void match(const char * file)
{
	FILE *input = fopen(file, "r");
	stack_t stack = stack_new(0);
	

	char iter;
	while (1)
	{
		iter = find(input);
		if (iter == ' ') {
			putc('\n', stdout);
			break;
		}

		if (stack_isempty(stack)) {
			if (isleft(iter)) {
				char *t = NEW(t); *t = iter; stack_push(stack, t);
			} else {
				fprintf(stderr, "Oops\n");
				return;
			}
		} else {
			char top = *(char*)stack_gettop(stack);
			if (compare(top, iter)) {
				stack_pop(stack);	
			} else {
				char *t = NEW(t); *t = iter; stack_push(stack, t);
			}
		}	
	} 	


	if (stack_isempty(stack)) {
		printf("OK\n");
	}	else {
		printf("Oops\n");
	}

}

int main(int argc, char **argv)
{
	assert(argc == 2);

	match(argv[1]);
	return 0;
}


